<?php
namespace app\m\controller;

use app\common\controller\HomeBase;
use org\SendMsg;
use TencentCloud\Captcha\V20190722\CaptchaClient;
use TencentCloud\Captcha\V20190722\Models\DescribeCaptchaResultRequest;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use think\Db;
use app\common\model\User as UserModel;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Session;
use think\facade\Url;

class Login extends HomeBase
{
    protected function initialize()
    {
        parent::initialize();
        $this->assign('page_type','');
    }

    public function index()
    {
        if (!empty($this->user_info))
        {
            return $this->redirect("/user");
        }
        $this->site_seo("home");
        return $this->fetch('index',['index_style' => 'user']);
    }

    /**
     * 发送验证码
     */
    public function send_code ()
    {
        if ($this->request->isPost())
        {
            try {

                $ticket = $_POST["ticket"];
                $randstr = $_POST["randstr"];
                $phone = trim($this->request->param("phone"));

                $cred = new Credential($this->siteConfig['tencent_keyid'], $this->siteConfig['tencent_secretkey']);
                $httpProfile = new HttpProfile();
                $httpProfile->setEndpoint("captcha.tencentcloudapi.com");

                $clientProfile = new ClientProfile();
                $clientProfile->setHttpProfile($httpProfile);
                $client = new CaptchaClient($cred, "", $clientProfile);

                $req = new DescribeCaptchaResultRequest();

                $params = array(
                    "CaptchaType" => 9,
                    "Ticket" => $ticket,
                    "UserIp" => $this->request->ip(),
                    "Randstr" => $randstr,
                    "CaptchaAppId" => (int)$this->siteConfig['tencent_captcha_appid'],
                    "AppSecretKey" =>  $this->siteConfig['tencent_app_secretkey']
                );
                $req->fromJsonString(json_encode($params));

                $resp = json_decode($client->DescribeCaptchaResult($req)->toJsonString(),true);
                if ($resp['CaptchaCode'] == 1 && $resp['CaptchaMsg'] == "OK")
                {
                    $sendMsg = new SendMsg($this->siteConfig['sms_api_keyid'],$this->siteConfig['sms_api_secret'],$this->siteConfig['sms_api_appid'],$this->siteConfig['sms_api_sign'],$this->siteConfig['sms_api_template_id']);

                    if (check_mobile_number($phone))
                    {
                        if (!strpos($phone,"+86")) {

                            $code = rand(1000,9999);
                            Cache::set("verify_code_" . md5($this->request->ip()),$code,60 * 5);

                            $data = $sendMsg->tencent("+86" . $phone,[(string)$code,'5']);

                            $data = $data['SendStatusSet'][0];
                            if ($data['Code'] == 'Ok' && $data['Message'] == "send success")
                            {
                                return $this->echoJson([],200,"短信发送成功");
                            }else {
                                return $this->echoJson([],-1,"短信发送失败");
                            }
                        }

                    }else {
                        return $this->echoJson([],-1,"手机号不正确");
                    }

                }

            }
            catch(TencentCloudSDKException $e) {
                echo $e;
            }
        }

    }

 

}
